import axios from 'axios'
import router from '@/router'
import { getToken, removeToken } from '@/utils/token.js'
const instance = axios.create({
  baseURL: 'http://chenziyu.xyz:8000/v1_0',
  timeout: 20000,
  // withCredentials: true
})
// 请求拦截器
instance.interceptors.request.use(config => {
  config.headers.Authorization = `Bearer ${getToken()}` || ''
  return config
}, err => {
  return Promise.reject(err)
})
// 响应拦截器
instance.interceptors.response.use(res => {
  return res.data
}, err => {
  if(err.response.status === 401){
    removeToken();
    // router.currentRoute 相当于 在vue文件内this.$route -> 当前路由对象信息
    // fullPath, 路由对象里完整路由路径#后面的一切
    router.replace(`/login?path=${router.currentRoute.fullPath}`)
  }
  return Promise.reject(err)
})

export default instance
